package com.game.frame.GameFrame.GameFrameMain;

import com.game.frame.PauseFrame;
import com.game.init.Main;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class GameKeyListener implements KeyListener {
    private final GamePanel gamePanel;
    private final Hero hero;
    private final GameWindow gameWindow;
    private PauseFrame pauseFrame;

    public GameKeyListener(GamePanel gamePanel, Hero hero) {
        this.gamePanel = gamePanel;
        this.hero = hero;
        this.gameWindow = gamePanel.getGameWindow();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_A:
                hero.setStatus(3); // 向左移动
                break;
            case KeyEvent.VK_D:
                hero.setStatus(4); // 向右移动
                break;
            case KeyEvent.VK_J:
                hero.attack();     // 攻击
                break;
            case KeyEvent.VK_SPACE:
                hero.setStatus(5); // 跳跃
                break;
            case KeyEvent.VK_R:
                // 重开
                gamePanel.getGameWindow().resetGame();
                break;
            case KeyEvent.VK_P:
                // 暂停
                this.pauseFrame = new PauseFrame();
                gameWindow.setVisible(false);
                // 将当前的GameWindow实例传递给PauseFrame
                pauseFrame.setGameWindow(gameWindow);
                break;
            case KeyEvent.VK_Y:
                // 继续
                if (pauseFrame != null) {
                    pauseFrame.dispose();
                    gameWindow.setVisible(true);
                }
                break;
            case KeyEvent.VK_N:
                // 退出
                if (pauseFrame != null) {
                    pauseFrame.dispose();
                    gameWindow.close();
                }
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_A:
            case KeyEvent.VK_D:
                hero.setStatus(0); // 停止移动
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // 不需要实现
    }
} 